const http = require('http');
const url = require('url');
const querystring = require('querystring');

http.createServer(function (request, response) {
  const parseUrl = url.parse(request.url);
  if (parseUrl.pathname === '/') {
    if (parseUrl.query) {
      response.writeHead(200, {'Content-Type': 'application/json'});
      const jsonObj = querystring.parse(parseUrl.query);
      response.end(JSON.stringify(jsonObj));
    } else {
      // 没有参数
      response.writeHead(200, {'Content-Type': 'text/plain'});
      response.end('Hello World');
    }
  }
}).listen(8081);

console.log('Server running at http://127.0.0.1:8081/');